import { director, Node, Scene, Color, v3 } from 'cc';

const _v30 = v3();

export namespace CocosTools {

    export function findRecursive(name: string, node?: Node | Scene) {
        node = node ?? director.getScene();
        let nodes = [];
        node.walk(_ => _.name == name && nodes.push(_));
        return nodes;
    }

    export function path(node: Node, names: string[] = []) {
        names.unshift(node.name);
        !!node.parent && path(node.parent, names);
        return names.join('/');
    }

    export function nextEnumItem(num: number, enumType: object,) {
        const length = Object.values(enumType).length;
        return (num + 1) % length;
    }

    export function isSameColor(c0: Color, c1: Color) {
        return c0.r == c1.r && c0.g == c1.g && c0.b == c1.b;
    }

    export function alignLocalPositionToIntegerRecursive(node: Node) {
        node.getPosition(_v30);
        _v30.x = Math.round(_v30.x);
        _v30.y = Math.round(_v30.y);
        _v30.z = Math.round(_v30.z);
        node.setPosition(_v30);
        node.children.forEach(_ => alignLocalPositionToIntegerRecursive(node));
    }

}